package com.applcn.user.repo.repository;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.applcn.user.constant.GenderEnum;
import com.applcn.user.repo.entity.UserEntity;
import com.applcn.user.repo.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * com.applcn.user.repo.repository
 * Description:
 *
 * @author: 大妖怪
 * @date: 2020-01-18 00:44
 */
@Repository
public class UserRepository extends ServiceImpl<UserMapper, UserEntity> {

    /**
     * 微信登录
     *
     * @param openid
     * @param nickName
     * @param gender
     * @param avatarUrl
     * @return
     */
    public UserEntity wxLogin(String openid, String nickName, Integer gender, String avatarUrl) {
        Assert.notBlank(openid, "openId不能为空！");
        GenderEnum genderEnum = GenderEnum.getGenderEnum(gender);
        Assert.notNull(genderEnum, "性别不正确！");
        UserEntity userEntity = baseMapper.selectOne(
                new QueryWrapper<UserEntity>()
                        .lambda()
                        .eq(UserEntity::getWxOpenId, openid)
        );

        if (userEntity == null) {
            userEntity = new UserEntity();
            userEntity.setWxOpenId(openid);
            userEntity.setGender(genderEnum);
            userEntity.setAvatarUrl(avatarUrl);
            userEntity.setNickName(nickName);
            baseMapper.insert(userEntity);
        } else {
            userEntity.setAvatarUrl(avatarUrl);
            userEntity.setGender(genderEnum);
            userEntity.setNickName(nickName);
            baseMapper.updateById(userEntity);
        }

        return userEntity;
    }

    /**
     * 通过手机号查询用户
     *
     * @param phone
     * @return
     */
    public UserEntity getByPhone(String phone) {
        UserEntity userEntity = baseMapper.selectOne(
                new QueryWrapper<UserEntity>()
                        .lambda()
                        .eq(UserEntity::getPhone, phone)
        );

        return userEntity;
    }

    /**
     * 创建用户
     *
     * @param userEntity
     */
    public void createUser(UserEntity userEntity) {
        if (StrUtil.isNotBlank(userEntity.getPassword())) {
            userEntity.setPassword(SecureUtil.md5(userEntity.getPassword()));
        }
        baseMapper.insert(userEntity);
    }

    /**
     * 通过手机号密码登录
     *
     * @param phone
     * @param password
     * @return
     */
    public UserEntity getByPhoneAndPassword(String phone, String password) {
        UserEntity userEntity = baseMapper.selectOne(
                new QueryWrapper<UserEntity>()
                .lambda()
                .eq(UserEntity::getPhone, phone)
                .eq(UserEntity::getPassword, SecureUtil.md5(password))
        );

        return userEntity;
    }

    /**
     * 搜索用户
     * @param nickName
     * @param phone
     * @return
     */
    public List<UserEntity> searchUserByName(String nickName, String phone) {
        LambdaQueryWrapper<UserEntity> wrapper = new QueryWrapper<UserEntity>().lambda();
        if (StrUtil.isNotBlank(nickName)) {
            wrapper.like(UserEntity::getNickName, nickName);
        }
        if (StrUtil.isNotBlank(phone)) {
            wrapper.eq(UserEntity::getPhone, phone);
        }
        return baseMapper.selectList(
                wrapper
        );
    }
}
